package com.yanqu.road.server.manger.palace;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.pb.estatewar.EstateWarProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.assistant.AssistantModule;
import com.yanqu.road.server.gameplayer.module.palace.PalaceModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

/**
==============================================
 *皇城
==============================================
* 作者：zrq
* 时间：2022/1/25
**/
public class PalaceActivityMgr extends TempMgr {
    private static final Object lockObj = new Object();
    private static PalaceActivity palaceActivity;
    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        if(null != palaceActivity){
            return palaceActivity.save();
        }
        return true;
    }

    public static PalaceActivity getPalaceActivity(int activityId){
        if(palaceActivity.getPalaceActivityId() == activityId){
            return palaceActivity;
        }
        return null;
    }

    public static PalaceActivity getInTimePalaceActivity(){
        if(null != palaceActivity){
            if(palaceActivity.isPalaceActivityInTime()){
                return palaceActivity;
            }
        }
        return null;
    }

    public static void reloadActivityData(){
        getLogger().info("PalaceActivityMgr reload activity start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.PalaceActivity.getValue());
        if (openActivityInfoList.size() <= 0) {
            palaceActivity = null;
            getLogger().info("no palace activity in show time.");
        }else {
            ActivityInfo activityInfo = openActivityInfoList.get(0);            //如果多个活动，取第一个，只能一个活动
            synchronized (lockObj) {
                if (null == palaceActivity || palaceActivity.getPalaceActivityId() != activityInfo.getActivityId()) {
                    PalaceActivity tempPalaceActivity = new PalaceActivity(activityInfo);
                    if (tempPalaceActivity.init()) {
                        palaceActivity = tempPalaceActivity;
                    } else {
                        getLogger().error("load palace activity fail, activityId:{}", activityInfo.getActivityId());
                    }
                } else {
                    if (!palaceActivity.reloadActivity(activityInfo)) {
                        getLogger().error("reload palace activity fail, activityId:{}", activityInfo.getActivityId());
                    }
                }
            }
        }

        // 在线玩家操作
        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            PalaceModule palaceModule = player.getModule(PalaceModule.class);
            if(null != palaceActivity) {
                palaceModule.syncActivityConfig();
            }
            palaceModule.palaceInit();
            //小玉助手的也处理一下
            AssistantModule assistantModule = player.getModule(AssistantModule.class);
            boolean isChange = assistantModule.checkPalaceMall();
            if(isChange){
                assistantModule.syncAssistantShopSet();
            }
        }
    }

    public static void sendPalaceAideSetting(int activityId, long userId, boolean onlyUnionMemberJoin, boolean needResponse){
        //发送到跨服
        EstateWarProto.PalaceAideJoinSettingCrossReqMsg.Builder crossMsg = EstateWarProto.PalaceAideJoinSettingCrossReqMsg.newBuilder();
        crossMsg.setActivityId(activityId);
        crossMsg.setUserId(userId);
        crossMsg.setNeedResponse(needResponse);
        crossMsg.setOnlyUnionMemberJoin(onlyUnionMemberJoin);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_PALACE_AIDE_JOIN_SETTING, crossMsg);
        GamePlayerMgr.sendPacket(userId, pbMsg);
    }
}
